﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_SaleList")]
    public class T_SaleList : IEntity<T_SaleList>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Title { get; set; }

        public int ResourceId { get; set; }

        public int GoodsCategoryID { get; set; }

        [Column(TypeName="decimal")]
        public decimal Price { get; set; }



        public override T_SaleList Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_SaleList Find(System.Linq.Expressions.Expression<Func<T_SaleList, bool>> where)
        {
            using (Context ctx = new Context()) 
            {
                var item = ctx.SaleList.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.GoodsCategoryID = item.GoodsCategoryID;
                    this.Price = item.Price;
                    this.ResourceId = item.ResourceId;
                    this.Title = item.Title;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_SaleList, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.SaleList.Any(where);
            }
        }
    }
}
